#coding: utf-8
import datetime
from lovewith.supplier.models import Supplier
from lovewith.analytics.models import ShopAnalytics


#店铺数据分析
class ShopAnalyticsApi():
    def __init__(self):
        pass


    #当天的浏览数据+1
    @staticmethod
    def increase(supplier_data):
        now = datetime.datetime.now()
        shop_analytics_data = ShopAnalytics.objects.filter(supplier_id=supplier_data.id)
        shop_analytics_data = shop_analytics_data.filter(date__year=now.year, date__month=now.month, date__day=now.day)

        if shop_analytics_data.exists():
            #当天的记录已存在
            shop_analytics = shop_analytics_data[0]
            shop_analytics.views += 1
            shop_analytics.save()
        else:
            #当天的记录不存在
            new_shop_analytics_data = ShopAnalytics(
                supplier=supplier_data,
                user=supplier_data.user,
                views=1
            )

            new_shop_analytics_data.save()


